using System;
using System.Collections.Specialized;
using NUnit.Framework;
using Snoop.Commons;

namespace Snoop.Commons.Tests
{
	[TestFixture]
	public class ServiceLocatorTest
	{
		[Test]
		public void AddAndGetInterfaceTest()
		{
			ServiceLocator.Instance.Add(typeof(IFoo), "Snoop.Commons.Tests.Foo, Snoop.Commons.Tests");
			object foo = ServiceLocator.Instance.Get(typeof(IFoo));
			Assert.IsInstanceOfType(typeof(IFoo),foo);
		}

		[Test]
		[ExpectedException("System.ArgumentException")]
		public void AddDuplicateTest()
		{
			string typeName = "Snoop.Commons.Tests.Foo, Snoop.Commons.Tests";
			ServiceLocator.Instance.Add(typeof(IFoo), typeName);
			object foo = ServiceLocator.Instance.Get(typeof(IFoo));
			Assert.IsInstanceOfType(Type.GetType(typeName) ,foo);
		}
		
		[Test]
		public void GetExpectedTypeTest()
		{
			string typeName = "Snoop.Commons.Tests.Foo, Snoop.Commons.Tests";
			ServiceLocator.Instance.Remove(typeof(IFoo));
			ServiceLocator.Instance.Add(typeof(IFoo), typeName);
			object foo = ServiceLocator.Instance.Get(typeof(IFoo));
			Assert.IsInstanceOfType(Type.GetType(typeName) ,foo);
		}

		[Test]
		public void GetEmptyTest()
		{
			object bar = ServiceLocator.Instance.Get(typeof(IBar));
			Assert.IsNull(bar);
		}
	}
}
